// : v.book.read.tip.statemachine.mouse.MouseTrapTest.java
// State Machine pattern using 'if' statements
// to determine the next state.
package v.book.read.tip.statemachine.mousetrap1;

import junit.framework.TestCase;
import v.book.read.tip.statemachine.Input;
import v.book.read.tip.statemachine.State;
import v.book.read.tip.statemachine.StateMachine;
import v.book.read.tip.statemachine.mouse.MouseAction;
import v.book.read.tip.statemachine.mouse.MouseMoveList;

import com.bruceeckel.util.StringList;

// A different subclass for each state:

class Waiting implements State {
    public void run() {
        System.out.println("Waiting: Broadcasting cheese smell");
    }

    public State next(Input i) {
        MouseAction ma = (MouseAction) i;
        if (ma.equals(MouseAction.appears)) return MouseTrap.luring;
        return MouseTrap.waiting;
    }
}


class Luring implements State {
    public void run() {
        System.out.println("Luring: Presenting Cheese, door open"); // 呈现奶酪
    }

    public State next(Input i) {
        MouseAction ma = (MouseAction) i;
        if (ma.equals(MouseAction.runsAway)) return MouseTrap.waiting;
        if (ma.equals(MouseAction.enters)) return MouseTrap.trapping;
        return MouseTrap.luring;
    }
}


class Trapping implements State {
    public void run() {
        System.out.println("Trapping: Closing door");
    }

    public State next(Input i) {
        MouseAction ma = (MouseAction) i;
        if (ma.equals(MouseAction.escapes)) return MouseTrap.waiting;
        if (ma.equals(MouseAction.trapped)) return MouseTrap.holding;
        return MouseTrap.trapping;
    }
}


class Holding implements State {
    public void run() {
        System.out.println("Holding: Mouse caught");
    }

    public State next(Input i) {
        MouseAction ma = (MouseAction) i;
        if (ma.equals(MouseAction.removed)) return MouseTrap.waiting;
        return MouseTrap.holding;
    }
}


class MouseTrap extends StateMachine {
    public static State waiting = new Waiting(),
            luring = new Luring(), // 引诱
            trapping = new Trapping(), // 捕获
            holding = new Holding(); // 抓住

    public MouseTrap() {
        super(waiting); // Initial state
    }
}


public class MouseTrapTest extends TestCase {
    MouseTrap trap = new MouseTrap();
    String filepath = "F:/OneDrive/coding/java/demo/book-read/src/main/java/v/book/read/tip/statemachine/mouse/MouseMoves.txt";
    MouseMoveList moves = new MouseMoveList(new StringList(filepath).iterator());

    public void test() {
        trap.runAll(moves.iterator());
    }

    public static void main(String args[]) {
        junit.textui.TestRunner.run(MouseTrapTest.class);
    }
} // /:~
